package com.kongyz.Test;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * @author kongyz
 * @date 2018/7/26 21:07
 * @Description:
 */
public class FixedLengthFrameDecoder extends ByteToMessageDecoder {

    private final int frameLength;

    public FixedLengthFrameDecoder(int frameLength) {
        if(frameLength<=0){
            throw new IllegalArgumentException("positive，长度必须是正整数"+frameLength);
        }
        this.frameLength = frameLength;
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        if(in.readableBytes()>=frameLength){
            ByteBuf buf=in.readBytes(frameLength);
            out.add(buf);
        }

    }
}
